<?php

class Cat_Tags_Php_Attr extends Cat_Processor_Monkey_TagBase
{
	function getOutput()
	{
		$args = $this->getArgs();
	
		if ( isset( $args['access'] ) )
		{
			$args['set'] = $args['get'] = $args['access'];
		}
		
		$defaults = array(
			'get' => 'public',
			'set' => 'public',
			'snippet.get' => '',
			'snippet.set' => '',
			'snippet.set.after' => '',
			'clean.lines' => true
		);
		
		$args = Cat_Common::withDefaults( $args, $defaults );
		
		$schema = array(
			0 => 'string',
			'get' => 'string',
			'set' => 'string'
		);
		
		Cat_Common::checkArgs( $args, $schema );
		
		$code = '';
		
		$snippet_get = trim( $args['snippet.get'] ) != '' ? "@@ snippet.print " . preg_replace( '/,/u', '; ', $args['snippet.get'] ) . " @@" : '';
		$snippet_set = trim( $args['snippet.set'] ) != '' ? "@@ snippet.print " . preg_replace( '/,/u', '; ', $args['snippet.set'] ) . " @@" : '';
		$snippet_set_a = trim( $args['snippet.set.after'] ) != '' ? "@@ snippet.print " . preg_replace( '/,/u', '; ', $args['snippet.set.after'] ) . " @@" : '';
		
		foreach( explode(',', $args[0]) as $attr )
		{
			$attr = trim($attr);			
			$attr_c = Cat_Strings::camelize( $attr );
			
			$code .= '
#
	private $_' . $attr . ";\n\n";
			
			if ( $args['get'] != 'none' )
			{
				$code .= '
#
	' . $args['get'] . ' function get' . $attr_c . "()
	{
			{$snippet_get}
			return \$this->_{$attr};
	}
";
			}
					
			if ( $args['set'] != 'none' )
			{
				$ass = '';
				
				if ( isset( $args['assert.type'] ) )
				{
					$ass = "@@ php.assert.type $attr; {$args['assert.type']} @@        ";
				}
				
				$code .= '
#
	' . $args['set'] . ' function set' . $attr_c . "( \$$attr )
	{
		$ass
		{$snippet_set}
		\$this->_{$attr} = \$$attr;
		{$snippet_set_a}
	}
";
			}
			
			$code .= "\n";
		}
		
		if ( $args['clean.lines'] )
		{
			$code = preg_replace( '/(\r\n|\n)\s+?\n/um', '$1', $code );
			$code = preg_replace( '/#\s+?\n/um', "\n", $code );
		}
		
		return $code;
	}
}